home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 10 / AACD 10.iso / AACD / Resources / Online / Term / Extras / Source / term-source.lha / CaptureParser.c < prev    next >
C/C++ Source or Header  |  1996-10-20  |  8KB  |  420 lines

  1. /*
  2. **    CaptureParser.c
  3. **
  4. **    Capture filter parser
  5. **
  6. **    Copyright © 1990-1996 by Olaf `Olsen' Barthel
  7. **        All Rights Reserved
  8. **
  9. **    :ts=4
  10. */
  11.  
  12. #ifndef _GLOBAL_H
  13. #include "Global.h"
  14. #endif
  15.  
  16.  
  17. /**********************************************************************/
  18.  
  19.  
  20. typedef BOOL (* SPECIAL_JUMP)(ParseContext *Context);
  21. typedef BOOL (* ABORT_JUMP)(ParseContext *Context,LONG Char);
  22.  
  23. STATIC SPECIAL_JUMP    *LocalSpecialTable;
  24. STATIC ABORT_JUMP    *LocalAbortTable;
  25.  
  26.  
  27. /**********************************************************************/
  28.  
  29.  
  30. STATIC BOOL
  31. AbortCancel(ParseContext *Context,LONG UnusedChar)
  32. {
  33.     Context->pc_InSequence        = FALSE;
  34.     Context->pc_CharsInBuffer    = 0;
  35.     Context->pc_ScanStep        = 0;
  36.  
  37.     return(FALSE);
  38. }
  39.  
  40. STATIC BOOL
  41. AbortEsc(ParseContext *Context,LONG c)
  42. {
  43.     AbortCancel(Context,c);
  44.  
  45.     Context->pc_InSequence = TRUE;
  46.  
  47.     return(TRUE);
  48. }
  49.  
  50. STATIC BOOL
  51. LocalParse(ParseContext *Context,LONG c)
  52. {
  53.     if(!Context->pc_ScanStep)
  54.     {
  55.         LONG i;
  56.  
  57.         for(i = 0 ; i < NumCodes ; i++)
  58.         {
  59.             if(ANSICode[i].FirstChar == c)
  60.             {
  61.                 if(ANSICode[i].ExactSize == 1)
  62.                 {
  63.                     Context->pc_CharsInBuffer = Context->pc_ScanStep = 0;
  64.  
  65.                     return(FALSE);
  66.                 }
  67.                 else
  68.                 {
  69.                     Context->pc_ScanStep = i;
  70.  
  71.                     Context->pc_SaveBuffer[Context->pc_CharsInBuffer++] = c;
  72.  
  73.                     Context->pc_Arnie = ANSICode[i].Terminator;
  74.  
  75.                     return(TRUE);
  76.                 }
  77.             }
  78.         }
  79.     }
  80.     else
  81.     {
  82.         if(Context->pc_CharsInBuffer < MAX_SCAN_SIZE)
  83.         {
  84.             if(Context->pc_Arnie)
  85.             {
  86.                 LONG i;
  87.  
  88.                 for(i = Context->pc_ScanStep ; i < NumCodes ; i++)
  89.                 {
  90.                     if(ANSICode[i].FirstChar == Context->pc_SaveBuffer[0])
  91.                     {
  92.                         if(Context->pc_Arnie[c])
  93.                         {
  94.                             Context->pc_CharsInBuffer = Context->pc_ScanStep = 0;
  95.  
  96.                             Context->pc_Arnie = NULL;
  97.  
  98.                             return(FALSE);
  99.                         }
  100.                         else
  101.                         {
  102.                             if(ANSICode[i].Match[c])
  103.                             {
  104.                                 Context->pc_ScanStep = i;
  105.  
  106.                                 Context->pc_SaveBuffer[Context->pc_CharsInBuffer++] = c;
  107.  
  108.                                 return(TRUE);
  109.                             }
  110.                         }
  111.                     }
  112.                 }
  113.             }
  114.             else
  115.             {
  116.                 LONG i;
  117.  
  118.                 for(i = Context->pc_ScanStep ; i < NumCodes ; i++)
  119.                 {
  120.                     if(ANSICode[i].FirstChar == Context->pc_SaveBuffer[0])
  121.                     {
  122.                         if(ANSICode[i].LastChar == c || (!ANSICode[i].LastChar && Context->pc_CharsInBuffer == 2 && ANSICode[i].ExactSize == 3))    /* Special case for VT52 */
  123.                         {
  124.                             Context->pc_CharsInBuffer = Context->pc_ScanStep = 0;
  125.  
  126.                             return(FALSE);
  127.                         }
  128.                         else
  129.                         {
  130.                             if(ANSICode[i].Match[c])
  131.                             {
  132.                                 Context->pc_ScanStep = i;
  133.  
  134.                                 Context->pc_SaveBuffer[Context->pc_CharsInBuffer++] = c;
  135.  
  136.                                 return(TRUE);
  137.                             }
  138.                         }
  139.                     }
  140.                 }
  141.             }
  142.         }
  143.     }
  144.  
  145.     Context->pc_CharsInBuffer = Context->pc_ScanStep = 0;
  146.  
  147.     Context->pc_Arnie = NULL;
  148.  
  149.     return(FALSE);
  150. }
  151.  
  152. STATIC BOOL
  153. AbortCSI(ParseContext *Context,LONG c)
  154. {
  155.     AbortCancel(Context,c);
  156.  
  157.     Context->pc_InSequence = TRUE;
  158.  
  159.     return(LocalParse(Context,'['));
  160. }
  161.  
  162.  
  163. /**********************************************************************/
  164.  
  165.  
  166. STATIC BOOL
  167. SpecialBackspace(ParseContext *Context)
  168. {
  169.     if(Context->pc_LineIndex)
  170.         Context->pc_LineIndex--;
  171.  
  172.     return(FALSE);
  173. }
  174.  
  175. STATIC BOOL
  176. SpecialReturn(ParseContext *Context)
  177. {
  178.     Context->pc_LineIndex = 0;
  179.  
  180.     return(FALSE);
  181. }
  182.  
  183. STATIC BOOL
  184. SpecialTab(ParseContext *Context)
  185. {
  186.     LONG Index = (Context->pc_LineIndex + 7) & ~8;
  187.  
  188.     if(Index == Context->pc_LineIndex)
  189.         Index += 8;
  190.  
  191.     if(Index > Context->pc_LineLen)
  192.         memset(&Context->pc_LineBuffer[Context->pc_LineLen],' ',Index - Context->pc_LineLen);
  193.  
  194.     Context->pc_LineIndex = Index;
  195.  
  196.     if(Index > Context->pc_LineLen)
  197.         Context->pc_LineLen = Index;
  198.  
  199.     return(FALSE);
  200. }
  201.  
  202. STATIC BOOL
  203. SpecialEsc(ParseContext *UnusedContext)
  204. {
  205.     return(TRUE);
  206. }
  207.  
  208. STATIC BOOL
  209. SpecialCSI(ParseContext *Context)
  210. {
  211.     return(LocalParse(Context,'['));
  212. }
  213.  
  214.  
  215. /**********************************************************************/
  216.  
  217.  
  218. VOID
  219. CaptureParserExit()
  220. {
  221.     FreeVecPooled(LocalSpecialTable);
  222.     FreeVecPooled(LocalAbortTable);
  223.  
  224.     LocalSpecialTable = NULL;
  225.     LocalAbortTable = NULL;
  226. }
  227.  
  228. BOOL
  229. CaptureParserInit()
  230. {
  231.     if(LocalSpecialTable = (SPECIAL_JUMP *)AllocVecPooled(256 * sizeof(SPECIAL_JUMP),MEMF_ANY | MEMF_CLEAR))
  232.     {
  233.         if(LocalAbortTable = (ABORT_JUMP *)AllocVecPooled(256 * sizeof(ABORT_JUMP),MEMF_ANY | MEMF_CLEAR))
  234.         {
  235.             STATIC struct { UBYTE Key; SPECIAL_JUMP Routine; } LocalSpecialKeys[] =
  236.             {
  237.                 '\b',    SpecialBackspace,
  238.                 '\r',    SpecialReturn,
  239.                 '\t',    SpecialTab,
  240.                 27,        SpecialEsc,
  241.                 155,    SpecialCSI
  242.             };
  243.  
  244.             LONG i;
  245.  
  246.             for(i = 0 ; i < NUM_ELEMENTS(LocalSpecialKeys) ; i++)
  247.                 LocalSpecialTable[LocalSpecialKeys[i].Key] = LocalSpecialKeys[i].Routine;
  248.  
  249.             for(i = 0 ; i < 256 ; i++)
  250.             {
  251.                 switch(AbortMap[i])
  252.                 {
  253.                     case 0:
  254.  
  255.                         LocalAbortTable[i] = LocalParse;
  256.                         break;
  257.  
  258.                     case 1:
  259.  
  260.                         LocalAbortTable[i] = AbortCancel;
  261.                         break;
  262.  
  263.                     case 2:
  264.  
  265.                         LocalAbortTable[i] = AbortEsc;
  266.                         break;
  267.  
  268.                     case 3:
  269.  
  270.                         LocalAbortTable[i] = AbortCSI;
  271.                         break;
  272.                 }
  273.             }
  274.  
  275.             return(TRUE);
  276.         }
  277.     }
  278.  
  279.     return(FALSE);
  280. }
  281.  
  282. ParseContext *
  283. CreateParseContext()
  284. {
  285.     return((ParseContext *)AllocVecPooled(sizeof(ParseContext),MEMF_ANY | MEMF_CLEAR));
  286. }
  287.  
  288.  
  289. /**********************************************************************/
  290.  
  291.  
  292.     /* CaptureParser(ParseContext *Context,STRPTR Buffer,LONG Size,COPTR OutputRoutine):
  293.      *
  294.      *    This is very similar to what happens in Emulation.c, in fact we're using the
  295.      *    same data. The basic difference is that no emulation routines are invoked.
  296.      *    Every single one is skipped, only the special control codes like form feed,
  297.      *    tab or carriage return survive. Eventually, we end up building single text
  298.      *    lines that are handed over to the supplied output callback routine. Are we
  299.      *    having fun?
  300.      */
  301.  
  302. VOID
  303. CaptureParser(ParseContext *Context,STRPTR Buffer,LONG Size,COPTR OutputRoutine)
  304. {
  305.     if(Size > 0)
  306.     {
  307.         LONG c,Mask;
  308.  
  309.         if(Config->SerialConfig->StripBit8)
  310.             Mask = 0x7F;
  311.         else
  312.             Mask = 0xFF;
  313.  
  314.         if(Context->pc_InSequence)
  315.         {
  316.             BOOL Result;
  317.  
  318.             do
  319.             {
  320.                 c = *Buffer++ & Mask;
  321.  
  322.                 Result = (*LocalAbortTable[c])(Context,c);
  323.             }
  324.             while(--Size > 0 && Result);
  325.  
  326.             Context->pc_InSequence = Result;
  327.         }
  328.  
  329.         if(Size > 0)
  330.         {
  331.             LONG BufferWidth = Config->CaptureConfig->BufferWidth - 1;
  332.  
  333.             if(Config->TerminalConfig->FontMode == FONT_STANDARD)
  334.             {
  335.                 do
  336.                 {
  337.                     c = (*Buffer++) & Mask;
  338.  
  339.                     if(Context->pc_InSequence)
  340.                         Context->pc_InSequence = (*LocalAbortTable[c])(Context,c);
  341.                     else
  342.                     {
  343.                         if(LocalSpecialTable[c])
  344.                             Context->pc_InSequence = (*LocalSpecialTable[c])(Context);
  345.                         else
  346.                         {
  347.                             if(c == '\n' || c == '\f' || c == '\v')
  348.                             {
  349.                                 (*OutputRoutine)(Context->pc_LineBuffer,Context->pc_LineLen);
  350.  
  351.                                 Context->pc_LineIndex = Context->pc_LineLen = 0;
  352.                             }
  353.                             else
  354.                             {
  355.                                 if(IsGlyph[c])
  356.                                 {
  357.                                     Context->pc_LineBuffer[Context->pc_LineIndex++] = c;
  358.  
  359.                                     if(Context->pc_LineIndex > Context->pc_LineLen)
  360.                                         Context->pc_LineLen = Context->pc_LineIndex;
  361.  
  362.                                     if(Context->pc_LineLen > BufferWidth)
  363.                                     {
  364.                                         (*OutputRoutine)(Context->pc_LineBuffer,Context->pc_LineLen);
  365.  
  366.                                         Context->pc_LineIndex = Context->pc_LineLen = 0;
  367.                                     }
  368.                                 }
  369.                             }
  370.                         }
  371.                     }
  372.                 }
  373.                 while(--Size > 0);
  374.             }
  375.             else
  376.             {
  377.                 do
  378.                 {
  379.                     c = (*Buffer++) & Mask;
  380.  
  381.                     if(Context->pc_InSequence)
  382.                         Context->pc_InSequence = (*LocalAbortTable[c])(Context,c);
  383.                     else
  384.                     {
  385.                         if(LocalSpecialTable[c])
  386.                             Context->pc_InSequence = (*LocalSpecialTable[c])(Context);
  387.                         else
  388.                         {
  389.                             if(c == '\n' || c == '\f' || c == '\v')
  390.                             {
  391.                                 (*OutputRoutine)(Context->pc_LineBuffer,Context->pc_LineLen);
  392.  
  393.                                 Context->pc_LineIndex = Context->pc_LineLen = 0;
  394.                             }
  395.                             else
  396.                             {
  397.                                 if(c)
  398.                                 {
  399.                                     Context->pc_LineBuffer[Context->pc_LineIndex++] = c;
  400.  
  401.                                     if(Context->pc_LineIndex > Context->pc_LineLen)
  402.                                         Context->pc_LineLen = Context->pc_LineIndex;
  403.  
  404.                                     if(Context->pc_LineLen > BufferWidth)
  405.                                     {
  406.                                         (*OutputRoutine)(Context->pc_LineBuffer,Context->pc_LineLen);
  407.  
  408.                                         Context->pc_LineIndex = Context->pc_LineLen = 0;
  409.                                     }
  410.                                 }
  411.                             }
  412.                         }
  413.                     }
  414.                 }
  415.                 while(--Size > 0);
  416.             }
  417.         }
  418.     }
  419. }
  420.